﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for ProviderCarMakeDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarMakeDB
    {
        public CarMakeDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public bool Delete(Int32 CarMakeId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarMakeDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CarMakeId", CarMakeId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }

        public Int32 Insert(CarMake providercarmake)
        {
            MyDatabase _db = new MyDatabase();
            Int32 _result = -1;
            string _spName = "sprocProviderCarMakeInfoInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();
            #region Parameter

            _sPara.Add("@ProviderCarMakeId", providercarmake.ProviderCarMakeId);
            _sPara.Add("@CarMakeId", providercarmake.CarMakeId);
            _sPara.Add("@ProviderCompanyId", providercarmake.ProviderCompanyId);
            _sPara.Add("@Description", providercarmake.Description);
            _sPara.Add("@RefCEUserId", providercarmake.RefCEUserId);
            _sPara.Add("@Status", providercarmake.Status);


            #endregion

            _result = Convert.ToInt32(_db.ExecuteSP(_spName, _sPara));

            return _result;
        }

        public CarMake GetProviderCarMake(Int32 CarMakeId, Int64 ProviderCompanyId)
        {
            CarMake _providercarmake = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarMakeSelectSingleItemByCarMakeId]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@CarMakeId", CarMakeId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _providercarmake = new CarMake();
                    _providercarmake = FillDataRecord(_dt, 0);
                }
            }
            return _providercarmake;
        }

        public CarMakeList GetProviderMakeList(Int64 ProviderCompanyId)
        {
            CarMakeList _Makelist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarMakeSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _Makelist = new CarMakeList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        CarMake _Make = new CarMake();
                        _Make = FillDataRecord(_dt, i);
                        _Makelist.Add(_Make);
                    }
                }
            }
            return _Makelist;
        }

        public CarMake GetProviderCarMakeItemByCarMakeId(Int32 CarMakeId, Int64 ProviderCompanyId)
        {
            CarMake _providercarmake = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarMakeSelectSingleItemByCarMakeId]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@CarMakeId", CarMakeId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _providercarmake = new CarMake();
                    _providercarmake = FillDataRecord(_dt, 0);
                }
            }
            return _providercarmake;
        }

        public CarMake GetProviderCarMakeItemByProviderCarMakeId(Int32 ProviderCarMakeId, Int64 ProviderCompanyId)
        {
            CarMake _providercarmake = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarMakeSelectSingleItemByProviderCarMakeId]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@ProviderCarMakeId", ProviderCarMakeId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _sPara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _providercarmake = new CarMake();
                    _providercarmake = FillDataRecord(_dt, 0);
                }
            }
            return _providercarmake;
        }

        public DataTable GetProviderCarMakeDT(Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("Result");
            string _spName = "sprocProviderCarMakeSelectItemList";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);            
            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        public DataTable SearchProviderCarMakeDT(string CarMake)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("Result");
            string _spName = "sprocProviderCarMakeSearchItem";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CarMakeDescription", CarMake);
            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        public DataTable GetProviderCXCarMakeDT(Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("Result");
            string _spName = "sprocProviderCXCarMakeSelectItemList";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        private CarMake FillDataRecord(DataTable myDataTable, int rowId)
        {
            CarMake myProviderCarMake = new CarMake();
            myProviderCarMake.ProviderCarMakeId = Convert.ToInt32(myDataTable.Rows[rowId]["ProviderCarMakeId"].ToString());
            myProviderCarMake.CarMakeId = Convert.ToInt32(myDataTable.Rows[rowId]["CarMakeId"].ToString());
            myProviderCarMake.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[rowId]["ProviderCompanyId"].ToString());
            myProviderCarMake.Description = myDataTable.Rows[rowId]["Description"].ToString();
            myProviderCarMake.RefCEUserId = Convert.ToInt64(myDataTable.Rows[rowId]["RefCEUserId"].ToString());
            myProviderCarMake.Status = Convert.ToBoolean(myDataTable.Rows[rowId]["Status"].ToString());
            return myProviderCarMake;
        }

    }
}